/*
 * @FilePath: /etws-sxk2307/src/Devices/Ina3221.cpp
 * @Description:  
 * @Author: ipk518 && 18163976442@163.com
 * @Date: 2024-04-10 11:33:11
 * @LastEditors: ipk518 18163976442@163.com
 * @LastEditTime: 2024-08-01 15:59:56
 * Copyright    : G AUTOMOBILE RESEARCH INSTITUTE CO.,LTD Copyright (c) 2024.
 */
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>   //define O_WRONLY and O_RDONLY 
#include <sys/ioctl.h>
#include "commHeader.h"
#include "Ina3221.h"


static int fd[HW_FILE_SIZE] = { 0 };


uint16_t hwBuff[HW_FILE_SIZE] = { 0 };

struct Ina3221 Ina3221e{
    "VCC_3V3",
    "/sys/class/hwmon/hwmon1/in1_input",
    "/sys/class/hwmon/hwmon1/curr1_input",
    "VCC_2V5",
    "/sys/class/hwmon/hwmon1/in2_input",
    "/sys/class/hwmon/hwmon1/curr2_input",
    "VDDR4_1V2",
    "/sys/class/hwmon/hwmon1/in3_input",
    "/sys/class/hwmon/hwmon1/curr3_input",
};

struct Ina3221 Ina3221a{
    "ZU9EG_MGTAVCC_0V9",
    "/sys/class/hwmon/hwmon2/in1_input",
    "/sys/class/hwmon/hwmon2/curr1_input",
    "ZU9EG_MGTAVTT_1V2",
    "/sys/class/hwmon/hwmon2/in2_input",
    "/sys/class/hwmon/hwmon2/curr2_input",
    "ZU9EG_VCCAUX_1V8",
    "/sys/class/hwmon/hwmon2/in3_input",
    "/sys/class/hwmon/hwmon2/curr3_input",
};

struct Ina3221 Ina3221c{
    "VCC_5V0",
    "/sys/class/hwmon/hwmon3/in1_input",
    "/sys/class/hwmon/hwmon3/curr1_input",
    "VOUT_5V6",
    "/sys/class/hwmon/hwmon3/in2_input",
    "/sys/class/hwmon/hwmon3/curr2_input",
    "HMC7044_3V6",
    "/sys/class/hwmon/hwmon3/in3_input",
    "/sys/class/hwmon/hwmon3/curr3_input",//17
};

struct Ina3221 Ina3221b{
    "ZU9EG_VCCINT_0V85",
    "/sys/class/hwmon/hwmon4/in1_input",
    "/sys/class/hwmon/hwmon4/curr1_input",
    "NONE",
    "/sys/class/hwmon/hwmon4/in2_input",
    "/sys/class/hwmon/hwmon4/curr2_input",
    "VCC_1V3",
    "/sys/class/hwmon/hwmon4/in3_input",
    "/sys/class/hwmon/hwmon4/curr3_input"
};


float plTemperature()
{
    float temp = 0,res_temp = 0;
 	FILE *fp;

 	fp = fopen(PL_TEMP, "r");
 	if(fp == NULL)
 	{
 		qlog_e("open %s failed!\n", PL_TEMP);
        fclose(fp);
        return 0;
 	}
 	auto ret = fscanf(fp,"%f",&temp);
    if(ret > 0) {
        res_temp = (float)((float)temp*509.314)/65536.0-280.23;
    }
 	// qlog_d("PS temperature0 : %f\n",((float)temp)*509.314/65536.0-280.23);
	ret = fclose(fp);
 	return res_temp;
}

float psTemperature()
{
    float temp = 0,res_temp = 0;
 	FILE *fp;

 	fp = fopen(PS_TEMP, "r");
 	if(fp == NULL)
 	{
 		qlog_e("open %s failed!\n", PL_TEMP);
        fclose(fp);
        return 0;
 	}
 	auto ret = fscanf(fp,"%f",&temp);
    if(ret > 0) {
        res_temp = (float)((float)temp*509.314)/65536.0-280.23;
    }
 	// qlog_d("PS temperature0 : %f\n",((float)temp)*509.314/65536.0-280.23);
    
	ret = fclose(fp);
 	return res_temp;
}

static int hwOpen(std::string filename)
{
    int file = open(filename.c_str(), O_RDONLY);
    if (file == -1) {
        qlog_e("Ina3221 %s fd open failed..",filename.c_str());
        close(file);
        file = -1;
    }
    return file;
}


void hwInit()
{
    Ina3221e.fd[0] = hwOpen(Ina3221e.suite1_voltage);
    Ina3221e.fd[1] = hwOpen(Ina3221e.suite1_current);
    Ina3221e.fd[2] = hwOpen(Ina3221e.suite2_voltage);
    Ina3221e.fd[3] = hwOpen(Ina3221e.suite2_current);
    Ina3221e.fd[4] = hwOpen(Ina3221e.suite3_voltage);
    Ina3221e.fd[5] = hwOpen(Ina3221e.suite3_current);

    Ina3221a.fd[0] = hwOpen(Ina3221a.suite1_voltage);
    Ina3221a.fd[1] = hwOpen(Ina3221a.suite1_current);
    Ina3221a.fd[2] = hwOpen(Ina3221a.suite2_voltage);
    Ina3221a.fd[3] = hwOpen(Ina3221a.suite2_current);
    Ina3221a.fd[4] = hwOpen(Ina3221a.suite3_voltage);
    Ina3221a.fd[5] = hwOpen(Ina3221a.suite3_current);

    Ina3221c.fd[0] = hwOpen(Ina3221c.suite1_voltage);
    Ina3221c.fd[1] = hwOpen(Ina3221c.suite1_current);
    Ina3221c.fd[2] = hwOpen(Ina3221c.suite2_voltage);
    Ina3221c.fd[3] = hwOpen(Ina3221c.suite2_current);
    Ina3221c.fd[4] = hwOpen(Ina3221c.suite3_voltage);
    Ina3221c.fd[5] = hwOpen(Ina3221c.suite3_current);

    Ina3221b.fd[0] = hwOpen(Ina3221b.suite1_voltage);
    Ina3221b.fd[1] = hwOpen(Ina3221b.suite1_current);
    Ina3221b.fd[2] = -1;
    Ina3221b.fd[3] = -1;
    Ina3221b.fd[4] = hwOpen(Ina3221b.suite3_voltage);
    Ina3221b.fd[5] = hwOpen(Ina3221b.suite3_current);
    
    fd[0] = Ina3221a.fd[0] ;
    fd[1] = Ina3221a.fd[1] ;
    fd[2] = Ina3221a.fd[2] ;
    fd[3] = Ina3221a.fd[3] ;
    fd[4] = Ina3221a.fd[4] ;
    fd[5] = Ina3221a.fd[5] ;

    fd[6] = Ina3221b.fd[0] ;
    fd[7] = Ina3221b.fd[1] ;
    fd[8] = Ina3221b.fd[4] ;
    fd[9] = Ina3221b.fd[5] ;

    fd[10] = Ina3221c.fd[0] ;
    fd[11] = Ina3221c.fd[1] ;
    fd[12] = Ina3221c.fd[2] ;
    fd[13] = Ina3221c.fd[3] ;
    fd[14] = Ina3221c.fd[4] ;
    fd[15] = Ina3221c.fd[5] ;

    fd[16] = Ina3221e.fd[0] ;
    fd[17] = Ina3221e.fd[1] ;
    fd[18] = Ina3221e.fd[2] ;
    fd[19] = Ina3221e.fd[3] ;
    fd[20] = Ina3221e.fd[4] ;
    fd[21] = Ina3221e.fd[5] ;

    
    qlog_i("Ina3221 init sucessful..");
    
}

int hwIna3221(struct Ina3221& src,uint16_t *data, uint16_t len)
{
    int val = 0,num = 0,size = 0;
    uint16_t m_buff[6] = { 0 };
    unsigned char vbuf[10];
    for (int i = 0; i < 6; i++) {
        if(src.fd[i] > 0){
            lseek(src.fd[i],0,SEEK_SET);
            val = read(src.fd[i], vbuf, sizeof(vbuf));
        }
        if(val > 0){
            // qlog_d(" %s: voltage(mv) = %s,current(ma) = %s\r\n",hwmon[j].p[k].name,vbuf, cbuf);
            m_buff[i] = atoi((const char*)vbuf);
            num++;
        } else {
            m_buff[i] = 0;
        }
        memset(vbuf,'\0',sizeof(vbuf));
    }
    size = (len > 6)? 6 : len;
    memcpy(data,m_buff,size);
    return num;
}


void hwHandle(struct Param& stc)
{
    uint8_t i = 0;
    int val = 0;
    unsigned char vbuf[10];
    // qlog_d("------------------------------------------------------------------\r\n");
    for (i = 0; i < HW_FILE_SIZE; i++) {
        if(fd[i] > 0){
            lseek(fd[i],0,SEEK_SET);
            val = read(fd[i], vbuf, sizeof(vbuf));
        }
        if(val > 0){
            // qlog_d(" %s: voltage(mv) = %s,current(ma) = %s\r\n",hwmon[j].p[k].name,vbuf, cbuf);
            hwBuff[i] = atoi((const char*)vbuf);
        } else {
            hwBuff[i] = 0;
        }
        memset(vbuf,'\0',sizeof(vbuf));
    }
    // qlog_d("------------------------------------------------------------------\r\n");
    memcpy((uint16_t *)&stc,hwBuff,sizeof(struct Param));
}


